package com.ren.service.core.rest;

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.ren.common.exception.BusinessException;
import com.ren.common.result.R;
import com.ren.common.result.ResponseEnum;
import com.ren.service.base.utils.JwtUtils;
import com.ren.service.core.model.vo.LoginReqVO;
import com.ren.service.core.model.vo.LoginRespVO;
import com.ren.service.core.model.vo.RegisterReqVO;
import com.ren.service.core.service.IUserInfoService;
import com.ren.common.constants.RedisKeyConstant;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author RenGaoshuai
 * @date 2023/8/8 10:19
 * @description
 */
@Api(tags = "会员接口")
@RestController
@RequestMapping("/api/core/userInfo")
@Slf4j
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 用户注册
     *
     * @param reqVO 入参
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody RegisterReqVO reqVO) {

        String mobile = reqVO.getMobile();
        String password = reqVO.getPassword();
        String code = reqVO.getCode();

        Assert.notNull(mobile, ResponseEnum.MOBILE_NULL_ERROR.getMessage());
        Assert.isTrue(PhoneUtil.isPhone(mobile), ResponseEnum.MOBILE_ERROR.getMessage());
        Assert.notNull(password, ResponseEnum.PASSWORD_NULL_ERROR.getMessage());
        Assert.notNull(code, ResponseEnum.CODE_NULL_ERROR.getMessage());

        //校验短信验证码
        String codeGen = (String) redisTemplate.opsForValue().get(RedisKeyConstant.SMS_CODE_KEY + mobile);
        if (!StrUtil.equals(code, codeGen)) {
            throw new BusinessException(ResponseEnum.CODE_ERROR);
        }

        userInfoService.register(reqVO);

        return R.ok().message("注册成功");
    }

    /**
     * 用户登录
     *
     * @param reqVO   入参
     * @param request request对象
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginReqVO reqVO, HttpServletRequest request) {
        String mobile = reqVO.getMobile();
        String password = reqVO.getPassword();
        Assert.notNull(mobile, ResponseEnum.MOBILE_NULL_ERROR.getMessage());
        Assert.notNull(password, ResponseEnum.PASSWORD_NULL_ERROR.getMessage());

        String remoteAddr = request.getRemoteAddr();

        LoginRespVO userInfoRespVO = userInfoService.login(reqVO, remoteAddr);

        return R.ok().data("userInfo", userInfoRespVO);

    }

    /**
     * 校验令牌
     */
    @GetMapping("/checkToken")
    public R checkToken(HttpServletRequest request) {

        String token = request.getHeader("token");
        boolean result = JwtUtils.checkToken(token);

        if (result) {
            return R.ok();
        } else {
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }
    }

    /**
     * 校验手机号是否被注册
     *
     * @param mobile
     * @return
     */
    @GetMapping("/checkMobile/{mobile}")
    public R checkMobile(@PathVariable("mobile") String mobile) {
        if (StrUtil.isBlank(mobile)) {
            throw new BusinessException("手机号不能为空");
        }
        boolean isRegister = userInfoService.checkMobile(mobile);
        return R.ok().data("isRegister", isRegister);
    }
}
